<script setup lang="ts">
	import { onLaunch, onShow, onHide, onShareAppMessage } from '@dcloudio/uni-app';
	import { useSettingStore } from '@/stores/index';

	const setting = useSettingStore();

	onShareAppMessage((res) => {
		if (res.from === 'button') {// 来自页面内分享按钮
			console.log(res.target)
		}
		const pages = getCurrentPages();
		const page : any = pages[pages.length - 1];
		const path = page.$page.fullPath;
		return {
			withShareTicket: false,
			title: '邀请您体验小程序',
			content: '源社区,你的社区生活管家',
			imageUrl: '',
			path: path + '?inviteUserId=' + setting.userInfo.phone,
			success: (res : any) => {

			},
			fail: (res : any) => {

			},
			complete: (res : any) => {

			}
		}
	})
	onLaunch(() => {
		const launchOptions = uni.getLaunchOptionsSync();
		const query = launchOptions.query;
		if(query.inviteUserId){
			setting.inviteUserId = query.inviteUserId
			uni.setStorage({
				key:'inviteUserId',
				data:query.inviteUserId
			})
		}
		// #ifdef MP
	
		const updateManager = uni.getUpdateManager();

		updateManager.onCheckForUpdate(function (res) {
			// 请求完新版本信息的回调
			console.log(res.hasUpdate);
		});

		updateManager.onUpdateReady(function (res) {
			uni.showModal({
				title: '更新提示',
				content: '新版本已经准备好，是否重启应用？',
				success(res) {
					if (res.confirm) {
						// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
						updateManager.applyUpdate();
					}
				}
			});
		});

		updateManager.onUpdateFailed(function (res) {
			// 新的版本下载失败
		});
		
		// #endif
		uni.getSystemInfo({
			success: (e : any) => {
				setting.platform = e.platform;
				// #ifndef H5
				setting.StatusBar = e.statusBarHeight;
				console.log('状态栏高度',setting.StatusBar)
				if (e.platform == 'android') {
					setting.CustomBar = setting.StatusBar + 50;
				} else {
					setting.CustomBar = setting.StatusBar + 45;
				}
				// #endif
				// #ifndef MP
				setting.StatusBar = e.statusBarHeight;
				if (e.platform == 'android') {
					setting.CustomBar = e.statusBarHeight + 50;
				} else {
					setting.CustomBar = e.statusBarHeight + 45;
				}
				// #endif

				// #ifdef MP-WEIXIN
				setting.StatusBar = e.statusBarHeight;
				let custom = wx.getMenuButtonBoundingClientRect();
				setting.Custom = custom;
				setting.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
				// #endif

				// #ifdef MP-ALIPAY
				setting.StatusBar = e.statusBarHeight;
				setting.CustomBar = e.statusBarHeight + e.titleBarHeight;
				// #endif
			}
		});
		setting.ColorList = [
			{
				title: '嫣红',
				name: 'red',
				color: '#e54d42'
			},
			{
				title: '桔橙',
				name: 'orange',
				color: '#f37b1d'
			},
			{
				title: '明黄',
				name: 'yellow',
				color: '#fbbd08'
			},
			{
				title: '橄榄',
				name: 'olive',
				color: '#8dc63f'
			},
			{
				title: '森绿',
				name: 'green',
				color: '#39b54a'
			},
			{
				title: '天青',
				name: 'cyan',
				color: '#1cbbb4'
			},
			{
				title: '海蓝',
				name: 'blue',
				color: '#0081ff'
			},
			{
				title: '姹紫',
				name: 'purple',
				color: '#6739b6'
			},
			{
				title: '木槿',
				name: 'mauve',
				color: '#9c26b0'
			},
			{
				title: '桃粉',
				name: 'pink',
				color: '#e03997'
			},
			{
				title: '棕褐',
				name: 'brown',
				color: '#a5673f'
			},
			{
				title: '玄灰',
				name: 'grey',
				color: '#8799a3'
			},
			{
				title: '草灰',
				name: 'gray',
				color: '#aaaaaa'
			},
			{
				title: '墨黑',
				name: 'black',
				color: '#333333'
			},
			{
				title: '雅白',
				name: 'white',
				color: '#ffffff'
			}
		];
	});
	onShow(() => {
		// #ifdef MP
		uni.showShareMenu({
			menus: ['shareAppMessage', 'shareTimeline']
		})
		// #endif

		try {
			const value = uni.getStorageSync('token');
			setting.setToken(value);
		} catch (e) {
			// error
		}
	});
	onHide(() => { });
</script>
<style>
	/*每个页面公共css */
	@import url('@/styles/colorui/main.css');
	@import url('@/styles/colorui/icon.css');
	@import url('@/styles/colorui/animation.css');
	@import url('@/styles/colorui/uinorm.css');
	@import '~@/styles/apps.scss';
</style>